package com.example.chat.service.impl;

import com.example.chat.model.AssetsRequestRecord;
import com.example.chat.model.Employee;
import com.example.chat.model.EquipMentType;
import com.example.chat.model.ro.AssetsRequestRecordRO;
import com.example.chat.repository.AssetsRequestRecordRepository;
import com.example.chat.repository.EmployeeRepository;
import com.example.chat.service.AssetsService;
import com.example.chat.utils.ChatException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description
 * @Date 2022/11/16 11:53
 * @Author pq
 */
@Service
@Slf4j
public class AssetsServiceImpl implements AssetsService {

    @Resource
    private EmployeeRepository employeeRepository;

    @Resource
    private AssetsRequestRecordRepository assetsRequestRecordRepository;

    @Override
    public void submitAssetsRequest(AssetsRequestRecordRO ro, String empId) {
        Employee submitEmp=employeeRepository.findByIdAndDeletedIsFalse(empId).orElseThrow(()->new ChatException("当前用户不存在"));
        Employee requestEmp=employeeRepository.findByIdAndDeletedIsFalse(ro.getEmpId()).orElseThrow(()->new ChatException("当前申请单的申请员工不存在"));
        Employee approvalEmp=employeeRepository.findByIdAndDeletedIsFalse(ro.getApprovalEmpId()).orElseThrow(()->new ChatException("当前申请单的审批员工不存在"));
        AssetsRequestRecord assetsRequestRecord=new AssetsRequestRecord();
        assetsRequestRecord.setApprovalEmp(approvalEmp);
        assetsRequestRecord.setSubmitEmp(submitEmp);
        assetsRequestRecord.setEmpName(requestEmp.getName());
        assetsRequestRecord.setRequestDate(ro.getRequestDate());
        assetsRequestRecord.setRequestUse(ro.getRequestUse());
        assetsRequestRecord.setDescription(ro.getDescription());
        assetsRequestRecordRepository.save(assetsRequestRecord);
    }
}
